package com.fishery.processing.controller;


import com.fishery.entity.Result;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Qualification;
import com.fishery.processing.entity.vo.CraftVo;
import com.fishery.processing.entity.vo.QualificationVo;
import com.fishery.processing.service.CraftService;
import com.fishery.processing.service.QualificationService;
import com.fishery.processing.util.ConvertUtil;
import com.fishery.processing.util.UpdateProxy;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Api(tags = "产品资质相关接口")
@CrossOrigin
@RestController
@RequestMapping("/processingFactory/qualification")
public class QualificationController {

    @Autowired
    private QualificationService qualificationService;

    @Autowired
    private UpdateProxy updateProxy;

    @Autowired
    private ConvertUtil convertUtil;

    @PostMapping("/")
    @ApiOperation("新增产品资质")
    public Result createQualification(@Valid @RequestBody @ApiParam(required = true,value = "qualificationVo")
                                              QualificationVo qualificationVo){
        return qualificationService.createQualification(convertUtil.convert(qualificationVo,Qualification.class)) == 1
                ? Result.success("新增成功") : Result.error("新增失败");
    }

    @PutMapping("/")
    @ApiOperation("修改产品资质")
    public Result updateQualification(@Valid @RequestBody @ApiParam(required = true,value = "qualificationVo")
                                                  QualificationVo qualificationVo){
        Qualification qualification = qualificationService.searchQualificationById(qualificationVo.getId());
        BeanUtils.copyProperties(qualificationVo,qualification);
        //使用反射对修改操作进行检查
        return ((QualificationService)(updateProxy.bind(qualificationService).getProxyInstance())).updateQualification(qualification) == 1
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除产品资质")
    public Result deleteQualification(@PathVariable(value = "id") String qualificationId){
        return qualificationService.deleteQualification(qualificationId) == 1
                ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据资质id获取产品资质")
    public Result searchQualificationById(@PathVariable(value = "id") String qualificationId){
        return Result.success().data(qualificationService.searchQualificationById(qualificationId));
    }

    @GetMapping("productQualification/{id}")
    @ApiOperation("根据加工产品id获取产品资质")
    public Result searchQualificationOfProduct(@PathVariable(value = "id") String productId){
        return Result.success()
                .data(convertUtil.convert(qualificationService.searchQualificationOfProduct(productId),QualificationVo.class));
    }
}

